package com.dd.admin.business.product.controller;

import cn.hutool.core.bean.BeanUtil;
import com.dd.admin.business.file.service.FileService;
import com.dd.admin.common.aop.operationLog.aop.OperLog;
import com.dd.admin.common.aop.operationLog.aop.OperType;
import com.dd.admin.common.security.SecurityUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.dd.admin.common.model.UpdateGroup;
import com.dd.admin.common.model.result.ResultBean;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.NotBlank;
import com.dd.admin.business.product.entity.Product;
import com.dd.admin.business.product.domain.ProductVo;
import com.dd.admin.business.product.domain.ProductDto;
import com.dd.admin.business.product.service.ProductService;

import java.util.List;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 商品 前端控制器
 * </p>
 *
 * @author 727869402@qq.com
 * @since 2024-05-23
 */
@Api(tags = "商品")
@RestController
public class ProductController {

    @Autowired
    ProductService productService;

    @Autowired
    FileService fileService;

    @ApiOperation(value = "商品-分页列表")
    @ApiOperationSupport(order = 1)
    @GetMapping("/admin/product/page")
    @OperLog(operModule = "获取商品列表",operType = OperType.QUERY,operDesc = "获取商品列表")
    public ResultBean<IPage<ProductVo>> page(ProductDto productDto) {
        productDto.setShopId(SecurityUtil.getLoginUser().getDeptId());
        IPage<ProductVo> pageInfo = productService.selectProductPage(productDto);
        pageInfo.getRecords().stream().forEach(productVo -> {
            String fileAuthUrl = fileService.getFileAuthUrl(productVo.getProductImage());
            productVo.setProductImageUrl(fileAuthUrl);
        });
        return ResultBean.success(pageInfo);
    }

    @ApiOperation(value = "商品-列表")
    @ApiOperationSupport(order = 2)
    @GetMapping("/admin/product/list")
    public ResultBean<List<ProductVo>> list(ProductDto productDto) {
        productDto.setShopId(SecurityUtil.getLoginUser().getDeptId());
        List<ProductVo> list = productService.selectProductList(productDto);
        list.stream().forEach(productVo -> {
            String fileAuthUrl = fileService.getFileAuthUrl(productVo.getProductImage());
            productVo.setProductImageUrl(fileAuthUrl);
        });
        return ResultBean.success(list);
    }

    @ApiOperation(value = "商品-添加")
    @ApiOperationSupport(order = 3)
    @PostMapping("/admin/product/add")
    @OperLog(operModule = "商品-添加",operType = OperType.ADD,operDesc = "商品-添加")
    public ResultBean<Product> add(@RequestBody @Validated ProductDto productDto) {
        Product product = BeanUtil.copyProperties(productDto, Product.class);
        productService.save(product);
        return ResultBean.success(product);
    }

    @ApiOperation(value = "商品-查询")
    @ApiOperationSupport(order = 4)
    @GetMapping("/admin/product/{productId}")
    public ResultBean<ProductVo> get(@PathVariable @NotBlank String productId) {
        Product product = productService.getById(productId);
        ProductVo productVo = BeanUtil.copyProperties(product,ProductVo.class);
        return ResultBean.success(productVo);
    }

    @ApiOperation(value = "商品-修改")
    @ApiOperationSupport(order = 5)
    @PostMapping("/admin/product/update")
    public ResultBean<Product> update(@RequestBody @Validated(UpdateGroup.class) ProductDto productDto) {
        Product product = BeanUtil.copyProperties(productDto, Product.class);
        productService.updateById(product);
        return ResultBean.success(product);
    }

    @ApiOperation(value = "商品-删除")
    @ApiOperationSupport(order = 6)
    @GetMapping("/admin/product/delete/{productId}")
    public ResultBean<Product> delete(@PathVariable @NotBlank String productId) {
        Boolean b = productService.removeById(productId);
        return ResultBean.success(b);
    }
}
